package com.xingkeduo.dto.transter.activitycouponlist;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xingkeduo.utils.DateUtils;

import lombok.Data;

@Data
public class Activity_Coupon_list {
	private String id;
    private String title;
    private String type;
    private String use_store;
    private String date;
    private String start_date;
    private String end_date;
    private String card_type;
    private String least_cost;
    private List<String> reduce_cost;
    private List<String> discount;
    private List<String> cant_use_reason;
    
    
	public void covert(JSONObject json) {
		this.id = json.getOrDefault("id", "")==null?"":json.getOrDefault("id", "").toString();
		this.title = json.getOrDefault("marketingName", "")==null?"":json.getOrDefault("marketingName", "").toString();
		this.type = "1";
		//this.use_store = json.getOrDefault("storeNames", "")==null?"":json.getOrDefault("storeNames", "").toString();;
		String storeIds = json.getOrDefault("storeIds", "")==null?"":json.getOrDefault("storeIds", "").toString();
		this.use_store="适用部分门店";
		if("0".equals(storeIds)) {
			this.use_store="适用所有门店";
		}
		Date startDate = json.getDate("useStartTime");
		if(startDate != null) {
			this.start_date = DateUtils.formatDate(startDate, DateUtils.DATE_FORMAT_V3);;
		}
		Date endDate = json.getDate("useEndTime");
		if(endDate != null) {
			this.end_date = DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT_V3);;
		}
		String serviceType = json.getOrDefault("serviceType", "")==null?"":json.getOrDefault("serviceType", "").toString();
		this.card_type =serviceType;
		String couponAmount = json.getOrDefault("couponAmount", "")==null?"":json.getOrDefault("couponAmount", "").toString();
		BigDecimal leastAmount = new BigDecimal(json.getOrDefault("leastCost", "0")==null?"0":json.getOrDefault("leastCost", "0").toString());
		leastAmount = leastAmount.subtract(leastAmount.setScale(1,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? leastAmount : leastAmount.setScale(1, BigDecimal.ROUND_DOWN);
		this.least_cost = leastAmount.toString();
		if(StringUtils.isBlank(couponAmount)) {
			this.reduce_cost = Lists.newArrayList("0","0");
		}else {
			BigDecimal amount = new BigDecimal(couponAmount);
			if("DISCOUNT".equalsIgnoreCase(serviceType)) {
				amount= amount.multiply(BigDecimal.valueOf(10)).setScale(1, BigDecimal.ROUND_DOWN);
				BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
				BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(0,BigDecimal.ROUND_DOWN);
				this.discount = Lists.newArrayList(one+"",two+"");
			}else{
				BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
				BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(1,BigDecimal.ROUND_DOWN);
				two = two.subtract(two.setScale(0,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? two : two.setScale(0, BigDecimal.ROUND_DOWN);
				this.reduce_cost = Lists.newArrayList(one+"",(two+"").replaceAll("\\.", ""));
			}
			
		}
		String unusableReason = json.getOrDefault("unusableReason", "")==null?"":json.getOrDefault("unusableReason", "").toString();
		if(StringUtils.isNotBlank(unusableReason)) {
			this.cant_use_reason = Lists.newArrayList(unusableReason);
		}
	}

}
